<?php
class category_filterModel extends baseModel{
    protected $table = 'filter_list';

    //多维分类
    public function filter_data($id,$cid,$app)
    {
        if(empty($id)){
            return;
        }
        //设置多维分类
        $get_array=in($_GET);
        $get=array();
        foreach ($get_array as $key => $value) {
            if(!in_array($key, array('r','cid','page'))&&!empty($value)){
                $get[$key]=$value;
            }
        }
        //处理html部分
        $filter_html='';
        $filter_list=$this->filter_list($id,100);
        if(empty($filter_list)){
            return;
        }
        foreach ($filter_list as $value) {
            $filter_html.='<ul> <li class="title">'.$value['name'].'</li>';
            $filter=$this->filter($value['fid'],100);
            $urlpar_str=urldecode($_GET[$value['urlname']]);
            if($value['type']){
                //多选
                $urlpar=explode('|', $urlpar_str);
            }else{
                $urlpar=array($urlpar_str);
            }
            if(empty($filter)){
                return;
            }
            foreach ($filter as $val) {
                $filter_html.='<li>';
                    //判断选中
                if(in_array($val['id'], $urlpar)){
                    if($value['type']){
                        if(substr($urlpar_str, 0,1)<>'|'){
                            $urlpar_str='|'.$urlpar_str;
                        }
                        $urlstr=str_replace('|'.$val['id'], '',$urlpar_str);
                    }else{
                        $urlstr='';
                    }
                    $selected='class="selected"';
                }else{
                    $urlstr='';
                    if($value['type']){
                        foreach ($urlpar as $par) {
                            $urlstr.=$par.'|';
                        }
                        $urlstr=$urlstr.$val['id'];
                    }else{
                        $urlstr=$val['id'];
                    }
                    $selected='';
                }
                $parameter=array_merge($get,array('cid'=>$cid,$value['urlname']=>trim($urlstr, '|')));
                $filter_html.='<a '.$selected.' href="'.url($app.'/category/index',$parameter).'">'.$val['name'].'</a>';
                $filter_html.='</li>';
            }
            $filter_html.='<div class="fn-clear"></div></ul>';
        }
            

        //处理条件部分
        if(!empty($get)){
            foreach ($get as $key => $value) {
                if(empty($value)){
                    continue;
                }
                $id_str='';
                $filter_info=$this->info($id,$key);
                if(!empty($filter_info)&&array_key_exists($filter_info['urlname'], $_GET)){
                    if($filter_info['type']==1){
                        $urlpar=explode('|', urldecode($_GET[$filter_info['urlname']]));
                        if(!empty($urlpar)){
							$id_str=implode(',',$urlpar);
                            $where.=' AND '.$filter_info['urlname'].'.id in ('.$id_str.')';
                        }
                    }else{
                        $where.=' AND '.$filter_info['urlname'].'.id = '.$value;
                    }
                }
            }
        }

        return array('html'=>$filter_html,'where'=>$where);

    }

    public function filter_sql($id=null)
    {
        if(empty($id)){
            return;
        }
        $filter_list=model('category_filter')->filter_list($id,100);
        if(empty($filter_list)){
            return;
        }
        foreach ($filter_list as $value) {
            if(array_key_exists($value['urlname'], $_GET)){
                $filter.=" JOIN {$this->model->pre}filter_relation {$value['urlname']} ON {$value['urlname']}.aid = A.aid ";
            }
        }
        return $filter;
    }

    public function filter_list($cid,$limit)
    {
        $loop="
            SELECT  *
             FROM {$this->model->pre}filter_list 
             WHERE cid={$cid} ORDER BY sequence asc,fid asc LIMIT {$limit}
            ";
            return $this->model->query($loop);
    }

    public function filter($fid,$limit)
    {
        $loop="
            SELECT *
             FROM {$this->model->pre}filter 
             WHERE fid={$fid} ORDER BY sequence asc,id asc LIMIT {$limit}
            ";
            return $this->model->query($loop);
    }

    public function info($cid,$urlname)
    {
        return $this->find('cid='.$cid.' AND urlname="'.$urlname.'"');
    }


}
